package com.itheima.controller;

import cn.hutool.core.date.DateTime;
import com.itheima.domain.EmpQueryParam;
import com.itheima.domain.PageResult;
import com.itheima.domain.Result;
import com.itheima.domain.pojo.Emp;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 查询所有员工
     */
/*    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult<Emp> pageResult = empService.getAll(page, pageSize);
        return Result.success(pageResult);
    }*/

    /**
     * 条件查询员工
     */
    @GetMapping
    public Result page(EmpQueryParam e) {
        PageResult<Emp> pageResult = empService.getAll(e);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody Emp emp){
        empService.save(emp);
        return Result.success();
    }

    /**
     * 查询全部员工
     */
    @GetMapping("/list")
    public Result getAll(){
        List<Emp> emps =empService.getEmps();
        return Result.success(emps);
    }

    /**
     * 批量删除员工
     * @param ids
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id){
        Emp e=empService.getById(id);
        return Result.success(e);
    }

    /**
     * 修改员工信息
     */
    @PutMapping
    public Result updateById(@RequestBody Emp emp){
        empService.updateById(emp);
        return Result.success();
    }
}
